package com.hopu.blb_erp.service.impl;

import com.hopu.blb_erp.entity.Laser;
import com.hopu.blb_erp.entity.PageEntity;
import com.hopu.blb_erp.entity.PageEntityP;
import com.hopu.blb_erp.entity.TPrinter;
import com.hopu.blb_erp.mapper.LaserMapper;
import com.hopu.blb_erp.mapper.TPrinterMapper;
import com.hopu.blb_erp.service.ILaserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author A-Zhang
 * @since 2021-11-23
 */
@Service
public class LaserServiceImpl extends ServiceImpl<LaserMapper, Laser> implements ILaserService {

    @Autowired
    private LaserMapper LaserMapper;
    @Autowired
    private TPrinterMapper tPrinterMapper;


//    @Override
//    public void addLaser(Laser Laser) {
//        LaserMapper.insertLaser(Laser);
//    }
//
//    @Override
//    public void removeLaser(Integer id) {
//        LaserMapper.deleteLaser(id);
//    }
//
//    @Override
//    public List<Laser> findAllLaser() {
//        return LaserMapper.selectAllLaser();
//    }


    @Override
    public Laser findLaserBylNo(String input) {
        return LaserMapper.selectLaserBylNo(input);
    }

    @Override
    public void removeLasers(List<Integer> ids) {
        LaserMapper.deleteLasers(ids);
    }

    @Override
    public void modifyLasers(List<Integer> ids) {
        TPrinter tPrinter = tPrinterMapper.selectById(ids.get(ids.size() - 1));
        for(int i = 0; i < ids.size() - 1; i++){
            Laser Laser = LaserMapper.selectById(ids.get(i));
            Laser.setLPrinter(tPrinter.getPNum());
            Laser.setLStatus("已打印");
            LaserMapper.updateById(Laser);
        }
    }

    @Override
    public void modifyLasers2(List<Integer> ids) {
        List<TPrinter> tPrinters = tPrinterMapper.selectList(null);
        int round;
        for(int i = 0; i < ids.size() ; i++){
            Laser Laser = LaserMapper.selectById(ids.get(i));
            round = (int) Math.round(Math.random() * (tPrinters.size() - 1));
            System.out.println(round);
            System.out.println(tPrinters.get(round));
            Laser.setLPrinter(tPrinters.get(round).getPNum());

            Laser.setLStatus("已打印");
            LaserMapper.updateById(Laser);
        }
    }
//
    @Override
    public ArrayList<Laser> selecLasersByStatus2(int status) {
        if(status==1){
            return LaserMapper.selecLasersByStatus();
        }else{
            return LaserMapper.selecLasersByStatus2();
        }
    }

    @Override
    public PageEntityP<Laser> findPage(Integer current) {
        //查询总数
        int count = LaserMapper.selectCount();
        //显示出来的页数
        int pageCount = count % PageEntityP.PAGE_SIZE == 0 ? count / PageEntityP.PAGE_SIZE :PageEntityP.PAGE_SIZE + 1;
        //使用limit查询分页

        List<Laser> sites = LaserMapper.selectPage((current - 1) * PageEntityP.PAGE_SIZE, PageEntityP.PAGE_SIZE);
        return new PageEntityP<>(sites,count,current,pageCount);
    }
}
